class Folder:
    
    def __init__(self, nombre):
        self.nombre = nombre
        self.parent = None
        self.folders = {}
        self.files = {} 
        
    def setParent(self,parent):
        self.parent = parent
        
    def getParent(self):
        return self.parent
        
    def isFolder(self):
        return True
    
    def writeFolder(self, path, folder,parent):
        if(self.isNotEmpty(path)):
            nextPath = self.nextPath(path)
            if(self.isNotEmpty(nextPath)):
                self.folders[self.getfirstPath(nextPath)].writeFolder(nextPath,folder,self)
            else:
                self.writeFolder(self.nextPath(path), folder,self)
        else:
            folder.setParent(folder)
            self.folders[folder.getName()] = folder
    
    def getName(self):
        return self.nombre
    
    def writeFile(self, path, file):
        if(self.isNotEmpty(path)):
            nextPath = self.nextPath(path)
            if(self.isNotEmpty(nextPath)):
                self.folders[self.getfirstPath(nextPath)].writeFile(nextPath,file)
            else:
                self.writeFile(self.nextPath(path), file)
        else:
            self.folders[file.getName()] = file
    
    def getfirstPath(self,path):
            if(self.lastPath(path)):
                return path
            else:
                return path[:path.find("/")]
    
    def nextPath(self, path):
        if(self.lastPath(path)):
            return ""
        else:
            rangeI = path.find("/") + 1
            ret = path[ rangeI : len(path) ]
            return ret
    
    def isNotEmpty (self, path):
        return (len (path) > 1)
    
    def lastPath(self, path):
        return path.find("/") == -1
    
    # Asume que vas por un File, pero si vas por un Folder o un File, la logica se complicaria un poco mas.
    def search(self,path):       
        if(not self.lastPath(path)):
            nextPath = self.nextPath(path)
            return self.folders[self.getfirstPath(nextPath)].search(self.nextPath(path))
        else:
            print("El archivo no se encuentra")
            
